package com.publicize.controller;

import com.publicize.commom.commonutils.JwtUtils;
import com.publicize.commom.commonutils.Result;
import com.publicize.entity.Examination;
import com.publicize.entity.Option;
import com.publicize.entity.Vo.ExaminationVo;
import com.publicize.entity.Vo.QueryVo;
import com.publicize.entity.Vo.option;
import com.publicize.service.ExaminationDetailService;
import com.publicize.service.ExaminationService;
import com.publicize.service.impl.ExaminationServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 * 知识竞赛测试表 前端控制器
 * </p>
 *
 * @author bs
 * @since 2021-03-05
 */
@RestController
@RequestMapping("/backstage_competition/examination")
@CrossOrigin
public class ExaminationController {
    @Resource
    private ExaminationService examinationService;
    //增加试题
    @PostMapping("insertOrUpdateExamination")
    public Result insertOrUpdateExamination(@RequestBody Examination examination){
        System.out.println(examination);
        Examination examinationn=examinationService.insertOrUpdate(examination);
        return Result.ok().data("examination",examinationn);
    }
    //增加试题excel
    @PostMapping("addExaminationExcel")
    public Result addExamination(@RequestBody MultipartFile file){
        String examinationNumber = getRandom620(10);
        Map<String, String> map = examinationService.save(examinationNumber, file);
        return Result.ok().data("map",map);
    }
    //根据试题编号删除试题
    @DeleteMapping("deleteExamination/{number}")
    public Result deleteExamination(@PathVariable("number") String number){
        boolean delete = examinationService.remove(number);
        return delete==true?Result.ok():Result.error();
    }
    //取消上传excel
    @PostMapping("UnuploadExcel/{number}")
    public Result UnuploadExcel(@PathVariable("number")String number){
        boolean remove=examinationService.unUpload(number);
        return remove==true?Result.ok():Result.error();
    }
    //生成随机数
    private static String getRandom620(Integer length) {
        String result = "";
        Random rand = new Random();
        int n = 20;
        if (null != length && length > 0) {
            n = length;
        }
        int randInt = 0;
        for (int i = 0; i < n; i++) {
            randInt = rand.nextInt(10);

            result += randInt;
        }
        return result;
    }
    //============================================================================================================================================
    //竞赛后台（竞赛+题目）
    @PostMapping("getExaminationList")
    public Result getExaminationList(@RequestBody QueryVo queryVo){
        List<ExaminationVo> examinationList = examinationService.getExaminationList(queryVo);
        int examinationCount = examinationService.getExaminationCount(queryVo);
        return Result.ok().data("examinationList",examinationList).data("examinationCount",examinationCount);
    }

    //新增竞赛
    @PostMapping("addExamination")
    public Result addExamination(@RequestBody Examination examination, HttpServletRequest request){
        System.out.println(examination);
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        examination.setManageId(memberIdByJwtToken);
        examinationService.save(examination);
        return Result.ok();
    }

}

